/*
 * @Author: liangkun 2669681641@qq.com
 * @Date: 2023-10-07 09:21:01
 * @LastEditors: liangkun 2669681641@qq.com
 * @LastEditTime: 2023-11-06 11:11:43
 * @FilePath: \radiation-hygiene-fe\src\utils\cipherDog\Syunew6W\callback.js
 * @Description: 
 * 
 * Copyright (c) 2023 by ${git_name_email}, All Rights Reserved. 
 */
import SoftKey6Ws from './NewSyunew6W'
// import NewSyunew6W from './NewSyunew6W'
let SoftKey6W = new SoftKey6Ws();


/**
 * 
 * @param {*} key 要设置的算法密钥一
 * 密钥为不超过32个的0-F字符，例如：1234567890ABCDEF1234567890ABCDEF,不足32个字符的，系统会自动在后面补0
 * @param {*} devicPath 设备路径
 */
const setCal_1 = function (key, devicPath) {
  return new Promise((resolve, reject) => {
    SoftKey6W.SetCal_2(key, devicPath)
    if (SoftKey6W.GetLastError() != 0) {
      reject("加密字符串出现错误");
    }
    resolve()
    // window.alert("已成功成功使用增强算法一对字符串进行加密，加密后的字符串为：" + outstring);
  })
}

/**
 * 
 * @param {*} key 要设置的算法密钥二
 * 密钥为不超过32个的0-F字符，例如：1234567890ABCDEF1234567890ABCDEF,不足32个字符的，系统会自动在后面补0
 * @param {*} devicPath 设备路径
 */
const setCal_2 = function (key, devicPath) {
  return SoftKey6W.SetCal_New(key, devicPath)
}

// 设置ukey账号
/**
 * 修改————————————————————————————————————————必看
 * 【账号长度位 -》 0  ，  账号 -》 多位  ，  密码位置 -》 账号长度位+账号长度+1   ，   密码 -》 密码位置+1】 
 * @returns 
 */
const setNameAndPass = function ({devicPath, name, password}) {
  return new Promise(async (resolve, reject) => {
    let addr = 0, nlength;
    {
      // 设置账号位置并返回账号长度
      nlength = await SoftKey6W.YWriteString(
        name,
        addr + 1,
        "ffffffff",
        "ffffffff",
        devicPath
      );
      if (SoftKey6W.GetLastError() != 0) {
        reject("写入用户名时错误。错误码：" + SoftKey6W.GetLastError().toString())
      }
  
      var LenBuf = new Uint8Array(1)
      LenBuf[0] = nlength;
      /**
       * SoftKey6W.YWrite(账号长度,起始位0，固定1，读密码1，读密码2，设备地址)
       */
      let ret = await SoftKey6W.YWrite(LenBuf, 0, 1, "ffffffff", "ffffffff", devicPath);
      if (ret != 0) {
        reject("写入用户名错误。错误码：" + ret.toString())
      }
    }
  
    {
      addr = nlength
      // 设置密码位置并返回账号长度     *第二参数为设置密码起始位（账号长度+账号长度起始位+密码长度起始位+1）
      let plength = await SoftKey6W.YWriteString(
        password,
        addr + 3,
        "ffffffff",
        "ffffffff",
        devicPath
      );
      if (SoftKey6W.GetLastError() != 0) {
        reject("写入密码错误。错误码：" + SoftKey6W.GetLastError().toString())
      }
      var LenBuf = new Uint8Array(1)
      // console.log(addr, plength)
      LenBuf[0] = plength;
      /**
       * SoftKey6W.YWrite(密码长度,密码起始位（账号长度+账号长度起始位+1），固定1，读密码1，读密码2，设备地址)
       */
      let ret = await SoftKey6W.YWrite(LenBuf, addr + 2, 1, "ffffffff", "ffffffff", devicPath);
      if (ret != 0) {
        reject("写入密码错误。错误码：" + ret.toString());
      }
    }
    resolve()
  })
}

// 设置自动跳转网站
const setWebUrl = function ({devicPath, url}) {
  return new Promise(async (resolve, reject) => {
  let addr = 1024, urlAddr = addr + 9,nlength;
    {

      // 
      let lengths = await SoftKey6W.YWriteString(
        'ebLON6A\u0001\u0014',
        addr + 1,
        "ffffffff",
        "ffffffff",
        devicPath
      );
      console.log(lengths);
      // 设置账号位置并返回账号长度
      nlength = await SoftKey6W.YWriteString(
        url + '                                                      ', // 想删除这串空白的试试   出bug算你的(❁´◡`❁)
        addr + lengths + 1,
        "ffffffff",
        "ffffffff",
        devicPath
      );
      if (SoftKey6W.GetLastError() != 0) {
        reject(
          "写入网址时错误。错误码：" + SoftKey6W.GetLastError().toString()
        );
      }

      // var LenBuf = new Uint8Array(1)
      // LenBuf[0] = nlength;
      // /**
      //  * SoftKey6W.YWrite(账号长度,起始位0，固定1，读密码1，读密码2，设备地址)
      //  */
      // let ret = await SoftKey6W.YWrite(LenBuf, urlAddr, 1, "ffffffff", "ffffffff", devicPath);
      // if (ret != 0) {
      //   reject("写入网址错误。错误码：" + ret.toString());
      // }
    }
    resolve()
  })
}

/**
 * 重置
 * @param {*} param0 
 * @returns 
 */
const resetUKey = function(devicPath) {
  let mSoftKey6w = new SoftKey6Ws();
  return new Promise(async (resolve, reject) => {
    let ret = await mSoftKey6w.ReSet(devicPath)
    if (ret != 0) {
      reject("初始化错误。错误码：" + JSON.stringify(ret));
    }
    resolve()
  })

}

/**
 * 读/写密码
 * @param {*} param0 
 * @returns 
 */
const setReaderAndWritePass = function(devicPath) {
  let mSoftKey6w = new SoftKey6Ws();
  return new Promise(async (resolve, reject) => {
    let read = await mSoftKey6w.SetReadPassword('00000000', '00000000', 'FFFFFFFF', 'FFFFFFFF' ,devicPath)
    if (read != 0) {
      reject("初始化读密码错误。错误码：" + read.toString());
    }

    let write = await mSoftKey6w.SetWritePassword('00000000', '00000000', 'FFFFFFFF', 'FFFFFFFF' ,devicPath)
    if (write != 0) {
      reject("初始化写密码错误。错误码：" + write.toString());
    }
    resolve()
  })
}

//写入字符串带长度
const setKeyData = function({devicPath, dataString}) {
  return new Promise(async (resolve, reject) => {
    let mSoftKey6w = new SoftKey6Ws();
    let nlen, addr = 200;
    {
      //写入字符串到地址1
      nlen = await mSoftKey6w.YWriteString(dataString, addr + 1, "ffffffff", "ffffffff", devicPath);
      if (mSoftKey6w.GetLastError() != 0) {
        reject("写入字符串(带长度)错误。");
      }
      var LenBuf = new Uint8Array(1)
      //写入字符串的长度到地址0
      LenBuf[0] = nlen;
      let ret = await mSoftKey6w.YWrite(LenBuf, addr, 1, "ffffffff", "ffffffff", devicPath);
      if (ret != 0) {
        reject("写入字符串长度错误。错误码：" + ret.toString());
      }
    }
    resolve()
  })
}

/**
 * 使用增强算法一写入字符串
 * @param {*} param0 
 * @returns 
 */
const setKeyData_1 = function({devicPath, dataString}) {
  return new Promise(async (resolve, reject) => {
    let mSoftKey6w = new SoftKey6Ws();
    let str, addr = 200;
    {
      //写入字符串到地址1
      str = await mSoftKey6w.EncString(dataString, devicPath);
      if (mSoftKey6w.GetLastError() != 0) {
        reject("写入字符串错误。");
      }
      var LenBuf = new Uint8Array(1)
      //写入字符串的长度到地址0
      LenBuf[0] = str;
      console.log(str);


      // let ret = await mSoftKey6w.YWrite(LenBuf, addr, 1, "ffffffff", "ffffffff", devicPath);
      // if (ret != 0) {
      //   reject("写入字符串长度错误。错误码：" + ret.toString());
      // }
    }
    resolve({str})
  })
}

export default {
  setCal_1,
  setCal_2,
  setNameAndPass,
  setWebUrl,
  setKeyData,
  setKeyData_1,
  resetUKey,
  setReaderAndWritePass
}